package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"grpc-common/ucenter/types/register"
	"ucenter/internal/config"
	"ucenter/internal/server"
	"ucenter/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

// 检查配置文件的读取路径是否正确
var configFile = flag.String("f", "etc/conf.yaml", "the config file")

func main() {
	flag.Parse() //解析文件
	logx.MustSetup(logx.LogConf{
		Stat:     false,
		Encoding: "plain",
	})

	var c config.Config //加载配置文件
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)

	//创建grpc服务: 地址,端口等都在配置文件中配置了
	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		register.RegisterRegisterServer(grpcServer, server.NewRegisterServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})

	defer s.Stop() //关闭服务

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start() //开启服务
}
